package com.ewanghuiju.app.widget.popupwindow;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.ewanghuiju.app.R;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author sj  qq:751943509
 * @class 隐私协议弹框
 * @time 2020/2/19
 */
public class PrivateXyPopup extends BasePopupWindow {
    @BindView(R.id.xieyi_text)
    TextView xieyiText;

    private XyPopupWindowCallback popupWindowCallback;

    public PrivateXyPopup(Context context, XyPopupWindowCallback popupWindowCallback) {
        super(context);
        ButterKnife.bind(this, getContentView());
        this.popupWindowCallback = popupWindowCallback;

        try {
            SpannableStringBuilder spannable = new SpannableStringBuilder("请你务必审慎阅读，充分理解\"服务协议“\"和“隐私政策\"各条款,包括但不限于:为了向你地理位置、内容分享等服务，我们需要收集你的设备信息、操作日志等个人信息。你可以在“设置“中查看，变更、删除个人信息并管理你的授权。你可阅读《服务协议》和《隐私政策》了解详细信息。如你同意，请点击同意开始接受我们的服务。");
            //设置文字的前景色
            spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(),R.color.colorPrimary)), 111, 117, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(),R.color.colorPrimary)), 118, 124, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //这个一定要记得设置，不然点击不生效
            xieyiText.setMovementMethod(LinkMovementMethod.getInstance());
            spannable.setSpan(new TextClick(), 111, 117, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new TextClick(), 118, 124, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            xieyiText.setText(spannable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_private_xy);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_sure})
    public void doClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_cancel:
                dismiss();
                if (popupWindowCallback != null) {
                    popupWindowCallback.cancelClickCallback();
                }
                break;
            case R.id.tv_sure:
                //确定按钮
                dismiss();
                if (popupWindowCallback != null) {
                    popupWindowCallback.sureClickCallback();
                }
                break;
            default:
                break;
        }
    }

    private class TextClick extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
            if (popupWindowCallback != null)
                popupWindowCallback.lookxyCallback();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
        }
    }

    public interface XyPopupWindowCallback {

        void sureClickCallback();

        void cancelClickCallback();

        void lookxyCallback();
    }
}
